/*********************************************************************************
 *      Copyright:  (C) 2021 Simply myself<1239726156@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  test.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2021年08月07日)
 *         Author:  Simply myself <1239726156@qq.com>
 *      ChangeLog:  1, Release initial version on "2021年08月07日 11时26分12秒"
 *                 
 ********************************************************************************/

#include "main.h"

/* ini配置文件的路径 */
#define DEFAULT_INI "../etc/conf.ini"

int g_run = 1;

/*  */
void sig_handler(int signum)
{
    if (signum == SIGINT)
    {
		printf ("\nSIGINT signal is captured\n");
		g_run = 0;
    }
}

/* main function */
int main(int argc, char **argv)
{
	int                  rv = 0;
	char                *prog_name = NULL;
	struct mosquitto    *mosq = NULL;
	zlog_category_t     *zc = NULL;
    mqtt_t               mqtt;
	float                temp;
	pthread_t            sub_tid;
	pthread_t            pub_tid;
	
	/* 清空结构体 */
	memset(&mqtt, 0, sizeof(mqtt));
	
	/* ini文件解析 */
	rv = ini_parser(DEFAULT_INI, &mqtt);
	if (rv < 0)
	{
		printf("[ERROR]: ini_parser failure: rv[%d]\n", rv);
		return -1;
	}
	printf("[SUCCESS]: ini_parser success\n");
	
	/* zlog配置文件解析 */
	rv = zlog_init(mqtt.zlog.z_name);
	if (rv)
	{
		printf ("[ERROR]: zlog init failure\n");
		return -2;
	}
	printf ("[SUCCESS]: zlog init success\n");
	
	/* 设置zlog的分类名为z_log */
	mqtt.zlog.zc = zlog_get_category("z_log");
	if (!mqtt.zlog.zc)
	{
		printf ("[ERROR]: zlog get cat failure\n");
		zlog_fini();
		return -3;
	}
	printf ("[SUCCESS]: zlog get cat success\n");
	zlog_notice (mqtt.zlog.zc, "[SUCCESS]: zlog get cat success");
	
	zc = mqtt.zlog.zc;
    
	/* 安装SIGINT信号（Ctrl+C） */
    signal(SIGINT, sig_handler);
	
	/* mosquitto库初始化 */
	rv = mosquitto_lib_init();
	if (rv != MOSQ_ERR_SUCCESS)
	{
		printf("[ERROR]: mosquitto_lib_init failure\n");
		zlog_error (zc, "mosquitto_lib_init failure");
		return -3;
	}
	printf("[SUCCESS]: mosquitto_lib_init success\n");
	zlog_notice (zc, "[SUCCESS]: mosquitto_lib_init success");
	
#if 1
	/*创建订阅子线程*/
	rv = thread_start(&sub_tid, sub_thread_func, (void *)&mqtt);
	if ( rv < 0 )
	{
		printf ("[ERROR]: Create Sub_Thread failure\n");
		zlog_error (zc, "Create Sub_Thread failure");
		rv = -4;
	}
	printf ("[SUCCESS]: Create Sub_Thread success\n");
	zlog_notice (zc, "[SUCCESS]: Create Sub_Thread success");
#endif
	
	/*创建发布子线程*/
#if 1
	rv = thread_start(&pub_tid, pub_thread_func, (void *)&mqtt);
	if ( rv < 0 )
	{
		printf ("[ERROR]: Create Pub_Thread failure\n");
		zlog_error (zc, "Create Pub_Thread failure");
		rv = -5;
	}
	printf ("[SUCCESS]: Create Pub_Thread success\n");
	zlog_notice (zc, "[SUCCESS]: Create Pub_Thread success\n");
#endif
	
	/* while循环，直到捕获到SIGINT信号 */
	while ( g_run )
	{
		;
	}
	
cleanup:

	printf ("\n**********Program Ending**********\n");
	zlog_notice (zc, "\n**********Program Ending**********\n");
	
	mosquitto_lib_cleanup();
	zlog_fini();
	
	return rv;
	
}  /* **********End of main function********** */

#if 0
void prog_usage(char *prog_name)
{
    printf("Usage %s [option]...\n", prog_name);
    printf("-c (--conf): The ini configuration file you want to use\n");
    printf("-h (--help): Display this help information\n");

    return ;
}
#endif


